'use strict';
import React from 'react';
import {
    View,
    ListView,
    RefreshControl,
} from 'react-native';
import ToolBar from '../Components/ToolBar';
import ImageViewer from './ImageViewer';
import Toast from '../Utils/toast';
import {_renderCommentItem} from '../Utils/ViewUtils';
import Global from '../Commom/GlobalConst';
import CommentItemView from '../Components/CommentItemView';
import RuralLibraryAPIService from "../Commom/RuralLibraryAPIService";
import FoundAPIService from '../Commom/FoundAPIService';
/**
 *  全部评论
 */
class AllCommentPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource: new ListView.DataSource({
                rowHasChanged: (r1, r2) => r1 !== r2,
            }),
            isRefreshing: false,
            record_count: 1,
            page: 1,
            allDataList: [],
        };
    }

    showImage(page) {
        this.props.navigator.push({
            name: 'ImageViewer',
            component: ImageViewer,
            passProps: {
                source: page
            }
        });
    }

    componentDidMount() {
        let commentList = this.props.commentList;
        let id = this.props.id;
        let type = this.props.type;
        if (this.props.id == null) {
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(commentList),
            });
        } else {
            if (type == 4) {
                RuralLibraryAPIService.getStoreComment(id, 1).then((ret)=> {
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(ret.result),
                        allDataList: ret.result,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                    });
                }).catch((err)=> {

                });
            }else if(type==5){
                FoundAPIService.getComment(id, 1).then((ret)=> {
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(ret.result),
                        allDataList: ret.result,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                    });
                }).catch((err)=> {

                });
            } else {
                RuralLibraryAPIService.allComments(id, type, 1).then((ret)=> {
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(ret.result),
                        allDataList: ret.result,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                    });
                }).catch((err)=> {

                });
            }
        }

    }

    //刷新数据
    onRefreshData() {
        let id = this.props.id;
        let type = this.props.type;
        this.setState({
            isRefreshing: true,
        });
        if (this.props.id == null) {
            this.setState({
                isRefreshing: false,
            });
        } else {
            if (type == 4) {
                RuralLibraryAPIService.getStoreComment(id, 1).then((ret)=> {
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(ret.result),
                        allDataList: ret.result,
                        isRefreshing: false,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                    });
                    Toast.show('刷新成功');
                }).catch((err)=> {
                    this.setState({
                        isRefreshing: false,
                    });
                });
            }else if(type==5){
                FoundAPIService.getComment(id, 1).then((ret)=> {
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(ret.result),
                        allDataList: ret.result,
                        isRefreshing: false,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                    });
                    Toast.show('刷新成功');
                }).catch((err)=> {

                });
            } else {
                RuralLibraryAPIService.allComments(id, type, 1).then((ret)=> {
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRows(ret.result),
                        allDataList: ret.result,
                        isRefreshing: false,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                    });
                    Toast.show('刷新成功');
                }).catch((err)=> {
                    this.setState({
                        isRefreshing: false,
                    });
                });
            }
        }
    }

    //加载更多数据
    loadMoreData() {
        let id = this.props.id;
        let type = this.props.type;
        if (this.props.id == null) {
            this.setState({
                isRefreshing: false,
            });
        } else {
            if (this.state.page < this.state.record_count) {
                this.setState({
                    isRefreshing: true,
                });
                if (type == 4) {
                    RuralLibraryAPIService.getStoreComment(id, this.state.page + 1).then((ret)=> {
                        let wanjian = [];
                        wanjian = this.state.allDataList;
                        for (var i = 0; i < ret.result.length; i++) {
                            wanjian.push(ret.result[i])
                        }
                        this.setState({
                            dataSource: this.state.dataSource.cloneWithRows(wanjian),
                            allDataList: wanjian,
                            isRefreshing: false,
                            record_count: ret.record_count,
                            page: Number(ret.page),
                        });
                        Toast.show('加载成功');
                    }).catch((err)=> {
                        this.setState({
                            isRefreshing: false,
                        });
                    });
                } else if(type==5){
                    FoundAPIService.getComment(id, this.state.page + 1).then((ret)=> {
                        let wanjian = [];
                        wanjian = this.state.allDataList;
                        for (var i = 0; i < ret.result.length; i++) {
                            wanjian.push(ret.result[i])
                        }
                        this.setState({
                            dataSource: this.state.dataSource.cloneWithRows(wanjian),
                            allDataList: wanjian,
                            isRefreshing: false,
                            record_count: ret.record_count,
                            page: Number(ret.page),
                        });
                        Toast.show('加载成功');
                    }).catch((err)=> {
                        this.setState({
                            isRefreshing: false,
                        });
                    });
                } else {
                    RuralLibraryAPIService.allComments(id, type, this.state.page + 1).then((ret)=> {
                        let wanjian = [];
                        wanjian = this.state.allDataList;
                        for (var i = 0; i < ret.result.length; i++) {
                            wanjian.push(ret.result[i])
                        }
                        this.setState({
                            dataSource: this.state.dataSource.cloneWithRows(wanjian),
                            allDataList: wanjian,
                            isRefreshing: false,
                            record_count: ret.record_count,
                            page: Number(ret.page),
                        });
                        Toast.show('加载成功');
                    }).catch((err)=> {
                        this.setState({
                            isRefreshing: false,
                        });
                    });
                }
            } else {
                if (this.state.page != 1) {
                    Toast.show('木有更多数据');
                }
            }
        }

    }

    _renderRow(rowData) {
        return <CommentItemView item={rowData} navigator={this.props.navigator}/>;
    }

    render() {
        return (
            <View style={{flex: 1,backgroundColor:'white'}}>
                <ToolBar leftFunc={()=> this.props.navigator.pop()} title="全部评论" rightFunc={()=> {
                }} rightImage={0}/>
                <ListView
                    refreshControl={
                                          <RefreshControl
                                            refreshing={this.state.isRefreshing}
                                            onRefresh={() => {this.onRefreshData()}}
                                            colors={['#4CAF50', '#4CAF50', '#4CAF50', '#4CAF50']}
                                            progressBackgroundColor="white"
                                          /> }
                    onEndReachedThreshold={100}
                    initialListSize={1}
                    onEndReached={() => {this.loadMoreData()}}
                    style={{borderTopWidth:2,borderColor:Global.Const.transparent}}
                    enableEmptySections={true}
                    dataSource={this.state.dataSource}
                    renderRow={(rowData) => this._renderRow(rowData)}
                />

            </View>
        );
    }
}
export default AllCommentPage;